﻿using MediatR;
using WarehouseManagement.Application.Command.WarehouseSAppCommand;
using WarehouseManagement.Domain.DTO.Allocate;
using WarehouseManagement.Domain.DTO.WarehouseSApp;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.AllocateCommandHandler
{
    public class ListWarehouseSAppCommandsHandler : IRequestHandler<ListWarehouseSAppCommands, List<ListWarehouseSAppDtos>>
    {
        private readonly lnterfaceWarehouseSAppRepository _warehouseSAppRepository;//仓库入库申请表
        private readonly lnterfaceStorageProductRepository _storageProductsRepository;//仓库入库申请产品明细表

        public ListWarehouseSAppCommandsHandler(lnterfaceWarehouseSAppRepository warehouseSAppRepository, lnterfaceStorageProductRepository storageProductsRepository)
        {
            _warehouseSAppRepository = warehouseSAppRepository;
            _storageProductsRepository = storageProductsRepository;
        }

        public async Task<List<ListWarehouseSAppDtos>> Handle(ListWarehouseSAppCommands request, CancellationToken cancellationToken)
        {
            var res = (await _warehouseSAppRepository.GetAll()).Where(x => x.IsDel == false).ToList();
            var product = (await _storageProductsRepository.GetAll()).ToList();
            var list = from a in res 
                       join b in product on a.StorageNumber equals b.StorageNumber
                       select new ListWarehouseSAppDtos
                       {
                           ShippRequestAppId = a.ShippRequestAppId,
                           StorageTopic = a.StorageTopic,
                           StorageNumber = a.StorageNumber,
                           StorageSource = a.StorageSource,
                           WName = a.WName,
                           AppName = a.AppName,
                           AppDate = a.AppDate,
                           StorageType = a.StorageType,
                           VName = a.VName,
                           StorageRemark = a.StorageRemark,
                           State = a.State,
                           StripNumber = a.StripNumber,
                           StorageProductId = b.StorageProductId,
                           PNumber = b.PNumber,
                           PName = b.PName,
                           PSpec = b.PSpec,
                           PUnit = b.PUnit,
                           PPrice = b.PPrice,
                           SPConfirmNum = b.SPConfirmNum,
                           SumPrice = b.SumPrice,
                           Batch = b.Batch,
                           CreateDate = b.CreateDate,
                           ValidDate = b.ValidDate,
                           ArriveDate = b.ArriveDate,
                           WrapName = b.WrapName,
                           Remark = b.Remark,
                           SumNum = b.SumNum
                       };
            return list.ToList();
        }
    }
}
